package br.edu.planodesaude.GUI.medico;

import javax.swing.JOptionPane;

import br.edu.planodesaude.controle.Alteradora;
import br.edu.planodesaude.controle.Pesquisadora;
import br.edu.planodesaude.dominio.Medico;
import br.edu.planodesaude.exceptions.SearchException;
import br.edu.planodesaude.exceptions.UpdateException;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author gustavo
 */
public class AlterarStatusMedico extends javax.swing.JInternalFrame {

    /**
     * Creates new form AlterarStatusMedico
     */
    public AlterarStatusMedico() {
    	setClosable(true);
    	setVisible(true);
    	setTitle("Alterar Status - Médico");
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        crmJTextField = new javax.swing.JTextField();
        buscarJButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nomeJTextField = new javax.swing.JTextField();
        situacaoJToggleButton = new javax.swing.JToggleButton();
        salvarJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("CRM");

        buscarJButton.setText("Buscar");
        buscarJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarJButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Nome");

        jLabel3.setText("Situação");

        situacaoJToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                situacaoJToggleButtonActionPerformed(evt);
            }
        });

        salvarJButton.setText("Salvar");
        salvarJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvarJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(situacaoJToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(crmJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buscarJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(nomeJTextField))
                    .addComponent(salvarJButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(crmJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1))
                            .addComponent(buscarJButton))
                        .addGap(51, 51, 51)
                        .addComponent(jLabel2))
                    .addComponent(nomeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(situacaoJToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addComponent(salvarJButton)
                .addContainerGap())
        );

        setBounds(455, 214, 458, 259);
    }// </editor-fold>                        

    private void salvarJButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	if(crmJTextField.getText().equals("")){
    		JOptionPane.showMessageDialog(null, "Preencha o campo CRM!", "Alerta!", JOptionPane.WARNING_MESSAGE);
    	}else{
	    	try{
	            new Alteradora().alterarMedico(crmJTextField.getText(), nomeJTextField.getText(), medico.getEspecialidades(), medico.isCredenciado());
	            JOptionPane.showMessageDialog(null, "Médico alterado com sucesso!", "Sucesso!", JOptionPane.INFORMATION_MESSAGE);
	            dispose();
	    	}catch(UpdateException uex){
	            JOptionPane.showMessageDialog(null, uex.getMessage(), "Alerta!", JOptionPane.WARNING_MESSAGE);
	    	}
    	}
    }                                             

    private void buscarJButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	if(crmJTextField.getText().equals("")){
    		JOptionPane.showMessageDialog(null, "Preencha o campo CRM!", "Alerta!", JOptionPane.WARNING_MESSAGE);
    	}else{
    		try{
    		medico = new Pesquisadora().pesquisarMedico(crmJTextField.getText());
    		crmJTextField.setEditable(false);
    		nomeJTextField.setText(medico.getNome());
    		nomeJTextField.setEditable(false);
    		if(medico.isCredenciado()){
                situacaoJToggleButton.setText("Credenciado");
    		}else{
                situacaoJToggleButton.setText("Descredenciado");
                situacaoJToggleButton.setSelected(true);
    		}
	    	}catch(SearchException sex){
	    		JOptionPane.showMessageDialog(null, sex.getMessage(), "Alerta!", JOptionPane.WARNING_MESSAGE);
	    	}
    	}
    }                                             

    private void situacaoJToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        medico.setCredenciado(!medico.isCredenciado());
        situacaoJToggleButton.setText(medico.isCredenciado() ? "Credenciado" : "Decredenciado");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AlterarStatusMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AlterarStatusMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AlterarStatusMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AlterarStatusMedico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AlterarStatusMedico().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton buscarJButton;
    private javax.swing.JTextField crmJTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField nomeJTextField;
    private javax.swing.JButton salvarJButton;
    private javax.swing.JToggleButton situacaoJToggleButton;
    private Medico medico;
    
    // End of variables declaration                   
}
